<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class M_scores extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    public function get_scores($gst_id) {
        $this->db->where('gst_id', $gst_id);
        //$this->db->where('student_id', $student_id);
        $q = $this->db->get('scores');
        $scores = array();
        foreach ($q->result_array() as $item) {
            $scores[$item['student_id']] = $item;
        }
        //var_dump($scores);

        return $scores;
    }

    public function edit_science_work($gst_id) {
        $sci1 = intval($this->input->post('sci1'));
        $sci2 = intval($this->input->post('sci2'));
        $data['sci1'] = $sci1 >= 0 ? $sci1 : 0;
        $data['sci2'] = $sci2 >= 0 ? $sci2 : 0;
        $student_id = $this->input->post('sci_student');
        $sql = "INSERT INTO scores (gst_id, student_id, scores_scientific1, scores_scientific2)
VALUES ('{$gst_id}','{$student_id}','{$data['sci1']}','{$data['sci2']}')
ON DUPLICATE KEY UPDATE
 gst_id='{$gst_id}',
 student_id='{$student_id}',
 scores_scientific1='{$data['sci1']}',
 scores_scientific2='{$data['sci2']}'";
        $this->db->query($sql);
    }

    public function edit_module($gst_id) {
        $test = intval($this->input->post('module_test'));
        $audit = intval($this->input->post('module_audit'));
        $data['scores_module_test'] = $test >= 0 ? $test : 0;
        $data['scores_module_audit'] = $audit >= 0 ? $audit : 0;
        $student_id = $this->input->post('module_student');
//$this->db->where('student_id', $student_id);
        // var_dump($data);
        $sql = "INSERT INTO scores (gst_id, student_id, scores_module_test, scores_module_audit)
VALUES ('{$gst_id}','{$student_id}','{$data['scores_module_test']}','{$data['scores_module_audit']}')
ON DUPLICATE KEY UPDATE
 gst_id='{$gst_id}',
 student_id='{$student_id}',
 scores_module_test='{$data['scores_module_test']}',
 scores_module_audit='{$data['scores_module_audit']}'";
        $this->db->query($sql);
    }

    public function edit_lectures($gst_id) {
        $module1_days = '';
        $module2_days = '';
        foreach ($_POST as $key => $val) {
            if ($val == 1) {
                $module1_days[$key] = 1;
            } else if ($val == 2) {
                $module2_days[$key] = 2;
            }
        }
        if (is_array($module1_days)) {
            $module1_days = array_keys($module1_days);
            $this->db->where('gst_id', $gst_id);
            $this->db->where_in('id', $module1_days);
            $this->db->update('subjects_days', array('module' => 1));
        }
        if (is_array($module2_days)) {
            $module2_days = array_keys($module2_days);
            $this->db->where('gst_id', $gst_id);
            $this->db->where_in('id', $module2_days);
            $this->db->update('subjects_days', array('module' => 2));
        }
    }

    public function edit_practices($gst_id) {
        $module1_days = '';
        $module2_days = '';
        foreach ($_POST as $key => $val) {
            if ($val == 1) {
                $module1_days[$key] = 1;
            } else if ($val == 2) {
                $module2_days[$key] = 2;
            }
        }
        if (is_array($module1_days)) {
            $module1_days = array_keys($module1_days);
            $this->db->where('gst_id', $gst_id);
            $this->db->where_in('id', $module1_days);
            $this->db->update('subjects_days', array('module' => 1));
        }
        if (is_array($module2_days)) {
            $module2_days = array_keys($module2_days);
            $this->db->where('gst_id', $gst_id);
            $this->db->where_in('id', $module2_days);
            $this->db->update('subjects_days', array('module' => 2));
        }
    }

    public function calculate_scores($student, $days, $visits, $scores, $info) {
        $st_id = $student['id'];
        $result = $student;
        $lect = array();
        $pract = array();
        $result['lect_m1'] = array();
        $result['lect_m2'] = array();
        $result['pract_m1'] = array();
        $result['pract_m2'] = array();
        foreach ($days as $day) {
            switch ($day['type']) { //lecture or practise lesson
                case 'l': {
                        $l = isset($visits[$st_id][$day['id']]) ? $visits[$st_id][$day['id']] : array('score' => 0);
                        switch ($day['module']) { //calculating number of lectures separate in module 1 and 2
                            case "1": {
                                    $result['lect_m1'][] = $l['score'];
                                }
                                break;
                            case "2": {
                                    $result['lect_m2'][] = $l['score'];
                                }
                                break;
                        }
                        $lect[] = $l;
                    }
                    break;
                case 'p': {
                        $p = isset($visits[$st_id][$day['id']]) ? $visits[$st_id][$day['id']] : array('score' => 0);
                        switch ($day['module']) {//calculating number of practices separate in module 1 and 2
                            case "1": {
                                    $result['pract_m1'][] = $p['score'];
                                }
                                break;
                            case "2": {
                                    $result['pract_m2'][] = $p['score'];
                                }
                                break;
                        }
                        $pract[] = $p;
                    }
                    break;
            }
        }
        $result['lect'] = $lect;
        $result['pract'] = $pract;

        $c = sizeof($result['lect_m1']); //module 1, lectures
        if ($c > 0) {
            $result['lect_m1'] = 3 * (array_sum($result['lect_m1']) / $c);
        } else
            $result['lect_m1'] = 0;
        $c = sizeof($result['lect_m2']); //module 2, lectures
        if ($c > 0) {
            $result['lect_m2'] = 3 * (array_sum($result['lect_m2']) / $c);
        } else
            $result['lect_m2'] = 0;

        $c = sizeof($result['pract_m1']); //module 1, practices
        if ($c > 0) {
            $result['pract_m1'] = 3 * (array_sum($result['pract_m1']) / $c);
        } else
            $result['pract_m1'] = 0;

        $c = sizeof($result['pract_m2']); //module 2, practices
        if ($c > 0) {
            $result['pract_m2'] = 3 * (array_sum($result['pract_m2']) / $c);
        } else
            $result['pract_m2'] = 0;
        $result['test_m1'] = isset($scores[$st_id]['scores_module_test']) ? $scores[$st_id]['scores_module_test'] : 0;
        $result['audit_m1'] = isset($scores[$st_id]['scores_module_audit']) ? $scores[$st_id]['scores_module_audit'] : 0;
        $result['mk1_1'] = $result['test_m1'] > 0 ? ($result['test_m1'] / 5) * 10 : 0;
        $result['mk1_2'] = $result['audit_m1'] > 0 ? ($result['audit_m1'] / 5) * 16 : 0;
        $result['mk1_3'] = $result['mk1_1'] + $result['mk1_2'];
        $result['mk2'] = $result['mk1_3'];
        $result['sam_m1'] = $result['audit_m1'] > 0 ? ($result['audit_m1'] / 5) * 12 : 0;
        $result['sam_m2'] = $result['sam_m1'];
        $result['sci_m1'] = isset($scores[$st_id]['scores_scientific1']) ? $scores[$st_id]['scores_scientific1'] : 0;
        $result['sci_m2'] = isset($scores[$st_id]['scores_scientific2']) ? $scores[$st_id]['scores_scientific2'] : 0;
        $result['module1'] = $result['lect_m1'] + $result['pract_m1'] + $result['mk1_3'] + $result['sam_m1'] + $result['sci_m1'];
        $result['module2'] = $result['lect_m2'] + $result['pract_m2'] + $result['mk2'] + $result['sam_m2'] + $result['sci_m2'];
        $result['total'] = $result['module1'] + $result['module2'];
        return $result;
        //var_dump($lect);
    }

}

/* End of file m_scores.php */
/* Location: ./application/models/m_scores.php */